#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*The input is assumed to be a 32-bit signed integer. 
 * Your function should return 0 when the reversed integer overflows.
 */
int reverse(int x) {
    char temp[12] = "";
    int i, j, y;
    int t = x;
    
    if(x<0)
    {
        t = 0 - x;
        if(t < 0)
            return 0;
        printf("\nminus t:%d, x:%d\r\n", t, x);
    }
        
    for(i=0, y=t; y>0; i++)
    {
        temp[i] = y%10;
        y /= 10;
    }
    
    for(j=0; j<i; j++)
    {
        if(j >= 9)
        {
            if((0x7FFFFFFF / y) < 10)
            {
                return 0;
            }
            else if(y * 10 > (0x7FFFFFFF - temp[j]))
            {
                return 0;
            }
        }
            
        if(j>0)
        {
            y *= 10;        
        }
       
        y += temp[j];    
        printf("\r\noutput, j:%d, y:%d\r\n", j, y);
    }
    
    if(x<0)
        y = 0 - y;
    
    return y;
}

int main(int argc, char **argv)
{
    int x = 1534236469;//-123;//-2147483648;//1534236469;
    
    printf("\r\nreverse %d is %d\r\n", x, reverse(x));
    
	printf("hello world\n");
	return 0;
}
